package com.scorpio.api.controller.goods;

import java.util.List;

import com.scorpio.common.core.page.TableDataInfo;
import com.scorpio.goods.vo.AddGoodsSpecVo;
import com.scorpio.goods.vo.UpdateInventoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.scorpio.common.annotation.Log;
import com.scorpio.common.core.controller.BaseController;
import com.scorpio.common.core.domain.AjaxResult;
import com.scorpio.common.enums.BusinessType;
import com.scorpio.goods.domain.GoodsSpecEntity;
import com.scorpio.goods.service.IGoodsSpecService;
import com.scorpio.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.scorpio.common.utils.verification.AddGroup;
import com.scorpio.common.utils.verification.DefaultGroup;
import com.scorpio.common.utils.verification.UpdateGroup;


/**
 * 商品规格属性Controller
 *
 * @author scorpio
 * @date 2021-11-01
 */
@RestController
@RequestMapping("/goods/goods_spec")
public class GoodsSpecController extends BaseController {
    @Autowired
    private IGoodsSpecService service;

    /**
     * 查询商品规格属性列表
     */
    @PreAuthorize("@ss.hasPermi('goods:goods_spec:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpecEntity goodsSpec) {
        startPage();
        List<GoodsSpecEntity> list = service.selectList(goodsSpec);
        return getDataTable(list);
    }

    /**
     * 获取商品规格属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:goods_spec:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(service.selectById(id));
    }

    /**
     * 新增商品规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:goods_spec:add')")
    @Log(title = "商品规格属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(value = {AddGroup.class, DefaultGroup.class}) AddGoodsSpecVo vo) {
        return toAjax(service.save(vo));
    }

    /**
     * 修改商品规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:goods_spec:edit')")
    @Log(title = "商品规格属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(value = {UpdateGroup.class, DefaultGroup.class}) AddGoodsSpecVo vo) {
        return toAjax(service.update(vo));
    }

    @PreAuthorize("@ss.hasPermi('goods:goods_spec:edit')")
    @Log(title = "商品修改库存", businessType = BusinessType.UPDATE)
    @PostMapping("/updateInventory")
    public AjaxResult updateInventory(@RequestBody @Validated UpdateInventoryVo vo) {
        return toAjax(service.updateInventory(vo));
    }


    /**
     * 删除商品规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:goods_spec:remove')")
    @Log(title = "商品规格属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.deleteByIds(ids));
    }
}
